package com.ruoyi.web.controller.stask;

import com.ruoyi.cat.domain.*;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 计划任务服务 - 负责处理文章和活动的收益分配
 */
@Slf4j
@RestController
@RequestMapping("/api/scheduleTask")
@Component
public class ScheduledDataTask {


    @Autowired
    private ICatIprecordTableService ipRecordService;


    @Autowired
    private ICatBasedataTableService baseDataService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private WritingsService writingsService;

    @GetMapping("/getTask1")
    public void testSingleArticleReward() {
        CatIprecordTable testRecord = new CatIprecordTable();
        testRecord.setShareUserId(127L);
        testRecord.setType("1");
        testRecord.setArticleId(96L);

        CatBasedataTable baseData = baseDataService.selectCatBasedataTableByIdRedis(1L);
        if (baseData == null) {
            log.error("未找到系统基础配置数据");
            return;
        }

        writingsService.processArticleReward(testRecord, baseData);
        log.info("测试文章收益计算完成");
    }

    @GetMapping("/getTask")
  //  @Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(cron = "0 50 23 * * ?")
    public void calculateShareRewards() {
        log.info("开始执行分享收益计算任务");
        try {
            // 获取系统基础配置
            CatBasedataTable baseData = baseDataService.selectCatBasedataTableByIdRedis(1L);
            if (baseData == null) {
                log.error("未找到系统基础配置数据");
                return;
            }

            // 获取昨日的分享记录数据
            List<CatIprecordTable> shareRecords = ipRecordService.getShareUserId();
            if (CollectionUtils.isEmpty(shareRecords)) {
                log.info("没有需要处理的分享记录");
                return;
            }

            // 处理每条分享记录
            AtomicInteger successCount = new AtomicInteger(0);
            for (CatIprecordTable record : shareRecords) {
                try {
                    // 根据类型分别处理文章和活动
                    if ("1".equals(record.getType())) {
                        writingsService.processArticleReward(record, baseData);
                    } else {
                        activityService.processActivityReward(record, baseData);
                    }
                    successCount.incrementAndGet();
                } catch (Exception ex) {
                    log.error("处理用户收益失败，用户ID：{}，记录类型：{}", record.getShareUserId(), record.getType(), ex);
                }
            }
            log.info("分享收益计算任务完成，成功处理{}条记录", successCount.get());
        } catch (Exception e) {
            log.error("分享收益计算任务执行异常", e);
        }
    }










}